package homework;

import java.util.Arrays;

public class MyArrayList {
    public int[] array;
    public int usedSize;
    public static final int Capacity = 50;


    //初始化顺序表
    public MyArrayList(int[] array, int usedSize) {
        this.array = new int[Capacity];
        this.usedSize = 0;
    }

    //打印顺序表
    public void print() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }

    //判断顺序表是否存储满了
    public boolean isFull() {
        return this.usedSize == this.array.length;
    }

    //在pos位置插入data
    public void add(int pos, int data) {
        if (isFull()) {
            this.array = Arrays.copyOf(this.array, 2 * this.array.length);
        }
        checkPos(pos);

        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.array[i + 1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;
    }

    //pos位置是否合法
    public void checkPos(int pos) {
        if (pos < 0 || pos > usedSize) {
            throw new RuntimeException("pos不合法！");
        }
    }

    //判断是否包含某个元素
    public boolean contain(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //判断顺序表是否为空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    //获取pos位置的元素
    public int getPos(int pos) {
        if (isEmpty()) {
            throw new RuntimeException("顺序表为空！");
        }
        if (pos < 0 || pos >= usedSize) {
            throw new RuntimeException("pos不合法！");
        }
        return this.array[pos];
    }

    //给pos位置的元素设置为value
    public void setPos(int pos, int value) {
        this.array[pos] = value;
    }

    //删除第一次出现的关键字key
    public void delete(int key) {
        int tmp = search(key);
        if (tmp == -1) {
            System.out.println("没有需要删除的元素！");
            return;
        }
        for (int i = tmp; i < this.usedSize - 1; i++) {
            this.array[i] = this.array[i + 1];
        }
        this.usedSize--;
    }

    //获取顺序表长度
    public int len() {
        return this.usedSize;
    }

    //清空顺序表
    public void clear() {
        this.usedSize = 0;
    }

    public static void main(String[] args) {
        int[] arr = new int[10];
        MyArrayList myArrayList = new MyArrayList(arr, 0);
        for (int i = 0; i < 10; i++) {
            myArrayList.add(i, i);
        }
        myArrayList.print();
        myArrayList.add(10, 66);
        myArrayList.print();
        System.out.println("====================");
        System.out.println(myArrayList.contain(66));
        System.out.println(myArrayList.search(9));
        System.out.println(myArrayList.getPos(4));
        //System.out.println(myArrayList.getPos(14));
        myArrayList.delete(0);
        myArrayList.print();
        myArrayList.delete(66);
        myArrayList.print();

        myArrayList.clear();
        myArrayList.print();
    }
}
